package com.ihunanren.biz.function.service.impl;

import static com.google.common.base.Strings.isNullOrEmpty;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ihunanren.biz.function.domain.SchoolCurriculum;
import com.ihunanren.biz.function.service.IWeixinService;
import com.ihunanren.dao.pojo.SchoolCurriculumDefinedDO;
import com.ihunanren.dao.pojo.SchoolCurriculumInfoDO;
import com.ihunanren.dao.weixin.ISchoolCurriculumDefinedDao;
import com.ihunanren.dao.weixin.ISchoolCurriculumInfoDao;

public class SchoolCurriculumService implements
		IWeixinService<SchoolCurriculum> {

	@Autowired
	private ISchoolCurriculumInfoDao schoolCurriculumInfoDao;
	@Autowired
	private ISchoolCurriculumDefinedDao schoolCurriculumDefinedDao;

	@Override
	public List<SchoolCurriculum> getResponse(String input) {

		if (isNullOrEmpty(input))
			return Arrays.asList(new SchoolCurriculum(null, null));

		try {
			String diyName = input.replace("课程表", "").replace("：", "")
					.replace(":", "").trim();
			if (isNullOrEmpty(diyName)) {
				return Arrays.asList(new SchoolCurriculum(null, null));
			}

			SchoolCurriculumDefinedDO curriculumDefinedDO = schoolCurriculumDefinedDao.getByDiyName(diyName);
			if (curriculumDefinedDO == null) {
				return Arrays.asList(new SchoolCurriculum(null, null));
			}
			List<SchoolCurriculumInfoDO> list = schoolCurriculumInfoDao.getSchoolCurriculumInfo(curriculumDefinedDO.getId());
			return Arrays.asList(new SchoolCurriculum(curriculumDefinedDO, list));
		} catch (Exception e) {
			Throwables.propagate(e);
		}
		return Arrays.asList(new SchoolCurriculum(null, null));
	}

}
